<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<TITLE>League Coolness ranking!</TITLE>
</HEAD>
<BODY>

<STYLE>
A {
	text-decoration: none;
	color: black;
}
A:hover {
	text-decoration: underline;
}
TABLE {
	border: 1px solid black;
	border-spacing: 0px;
	border-collapse: collapse;
}
TD.graph {
	font-size: 8;
	font-family: sans-serif;
	text-align: center;
	border: 1px solid black;
}

DIV.kader {
	border: 1px dotted black;
	padding: 10px;
}

IMG.flag {
	border: 1px solid black;
}
</STYLE>
<?php

require_once("database.inc.php");
require_once("track.inc.php");

$dbh=connect_db();

$globalactive = 0;
$globalusers = mysql_query("SELECT sum(activeusers) as active, sum(waitingusers) as waiting, sum(activeusers)+sum(waitingusers) as total FROM leagues", $dbh);
while ($globaluser = mysql_fetch_array($globalusers)) {
	$globalactive = $globaluser['active'];
}

$visited = array();

if (!$_GET['t']) {
	die ("list of teams currently hosted (not implemented yet)\n");
} else {
	$visits = mysql_query("SELECT *, UNIX_TIMESTAMP(DateVisited) as timestamp FROM visited WHERE TeamID = {$_GET['t']}", $dbh);
	if (!mysql_num_rows($visits)) {
		echo ("that team has not entered any visits here!\n");
	} else {
		while ($visit = mysql_fetch_array($visits)) {
			$visited[$visit['LeagueID']]['HomeTeamID'] = $visit['HomeTeamID'];
			$visited[$visit['LeagueID']]['HomeTeamName'] = $visit['HomeTeamName'];
			$visited[$visit['LeagueID']]['DateVisited'] = $visit['DateVisited'];
			$visited[$visit['LeagueID']]['timestamp'] = $visit['timestamp'];
			$visited[$visit['LeagueID']]['AwayGoals'] = $visit['AwayGoals'];
			$visited[$visit['LeagueID']]['HomeGoals'] = $visit['HomeGoals'];
		}
	}
}

$i = 0;
$score = 0;
$numscored = 0;
$coolest = "";
$coolestscore = 0;
$totalcool = 0;
$countrylist = array();


$hardtogetleagues = mysql_query("SELECT leagues.leagueid as leagueid, tlds.tld as tld, tlds.ciatld as cia, englishname, shortname, season, activeusers FROM `leagues`, `tlds` where leagues.leagueid = tlds.leagueid ORDER BY activeusers ", $dbh);
echo "<TABLE border=1>\n\t<TR><TD><B>English / local country name (country code)</B></TD><TD><B>Flag</B></TD><TD><B>Visited Team<B></TD><TD><B>Date</B></TD><TD><B>Score</B></TD><TD><B>Coolness index</B></TD></TR>\n";
while ($hardtogetleague = mysql_fetch_array($hardtogetleagues)) {
	$i++;
	$coolness = round($globalactive / $hardtogetleague['activeusers'], 2);
	$beenthere = false;
	if (isset($visited[$hardtogetleague['leagueid']])) {$beenthere = true; $countrylist[] = $hardtogetleague['tld'];}
	if (($coolness > $coolestscore) && $beenthere) {
		$coolestscore = $coolness;
		$coolest = $hardtogetleague['englishname'];
	}		
/*	if ($coolness > 20000) {
		$denom = "Brand spanking cool!";
	} else if ($coolness > 10000) {
		$denom = "Divinely cool";
	} else {
		$denom = "Could be cooler";
	}
*/
	echo "\t<TR";
	if ($beenthere) {
		if ($visited[$hardtogetleague['leagueid']]['timestamp'] > time()) {
			echo " bgcolor=\"#FFFF99\"";
		} else {
			echo " bgcolor=\"#CCFFCC\"";
		}
		echo "><TD>$i. <A HREF=\"/maptrick/?leagueid={$hardtogetleague['leagueid']}\">{$hardtogetleague['englishname']} / {$hardtogetleague['shortname']}</A> ({$hardtogetleague['tld']})</TD>
		<TD ALIGN=\"center\"><A HREF=\"http://www.cia.gov/cia/publications/factbook/flags/{$hardtogetleague['cia']}-flag.html\" TARGET=\"_new\"><IMG CLASS=\"flag\" SRC=\"./flags/{$hardtogetleague['leagueid']}flag.gif\"></A></TD>
		<TD ALIGN=\"center\"><A HREF=\"http://www.databased.at/hattrick/x-files/pages/teamdisplay.asp?teamID={$visited[$hardtogetleague['leagueid']]['HomeTeamID']}\">{$visited[$hardtogetleague['leagueid']]['HomeTeamName']}</A></TD>
		<TD ALIGN=\"center\">{$visited[$hardtogetleague['leagueid']]['DateVisited']}</TD>
		<TD ALIGN=\"center\">{$visited[$hardtogetleague['leagueid']]['HomeGoals']} - {$visited[$hardtogetleague['leagueid']]['AwayGoals']}</TD>
		<TD>$coolness</TD>";
		$score+=$coolness;
		$numscored++;
	} else {
		echo "><TD>$i. <A HREF=\"/maptrick/?leagueid={$hardtogetleague['leagueid']}\">{$hardtogetleague['englishname']} / {$hardtogetleague['shortname']}</A> ({$hardtogetleague['tld']})</TD>
		<TD ALIGN=\"center\"><A HREF=\"http://www.cia.gov/cia/publications/factbook/flags/{$hardtogetleague['cia']}-flag.html\" TARGET=\"_new\"><IMG CLASS=\"flag\" SRC=\"./flags/{$hardtogetleague['leagueid']}flag.gif\"></A></TD>
		<TD></TD>
		<TD></TD>
		<TD></TD>
		<TD>$coolness</TD>";
	}
	$totalcool += $coolness;
	echo "</TD></TR>\n";
}
echo "</TABLE><BR/>\n<BR/>\n";
$totalcool = round(($totalcool), 2);

if ($coolestscore > 0) {
	echo "Countries visited: ";
	sort($countrylist);
	for ($counter = 0; $counter <= count($countrylist)-1; $counter++) {
		echo "$countrylist[$counter]";
		if ($counter < count($countrylist)-1) {echo "-";}
	}
	echo "<BR/>\n";
	$coolestpart = round(($coolestscore / $score) * 100, 1);
	echo "The coolest country you have visited is $coolest, which is responsible for $coolestpart% of your total score.<br/>\n<br/>\n";
	$averagecool = round ($score / $numscored);
	$averagemanagers = round ($globalactive / ($score / $numscored));
	$averageleague = "nowhere";
	$averageleagues = mysql_query("SELECT englishname FROM leagues WHERE activeusers <= $averagemanagers ORDER BY activeusers DESC LIMIT 1", $dbh);
	while ($field = mysql_fetch_array($averageleagues)) {
		$averageleague = $field['englishname'];
	}
	echo "Your average coolness is $averagecool; this puts you on or below $averageleague.<BR/>\n";
}
$progress = round(($score / $totalcool) * 100, 1);
echo "The sum of all scores is $totalcool; currently, your score is $progress% of this sum.<br/>\n<br/>\n";

?>
<BR/>
<DIV class="kader">
We recommend<br/>
<a href="http://www.spreadfirefox.com/?q=affiliates&id=11638&t=52"><img border="0" alt="Get Firefox!" title="Get Firefox!" src="http://www.spreadfirefox.com/community/images/affiliates/Banners/120x600/trust.png"/></a><br/>
For better standards conformation and easier browsing</DIV>

</FORM>
</BODY>
</HTML>
